package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.Mode;
import ru.spb.osll.myfit.client.event.PageButtonEvent.PageButtonEventType;
import ru.spb.osll.myfit.client.view.common.TitleFormatter;
import ru.spb.osll.myfit.client.widget.*;

public class ToolBar extends Composite {
  protected final HorizontalPanel m_mainPanel = new HorizontalPanel();

  private Grid m_changeModePanel;

  private PageButton m_editButton;

  private PageButton m_viewButton;

  private Grid m_EditViewPanel;

  protected HorizontalPanel m_editToolPanel;

  protected HorizontalPanel m_viewToolPanel;

  protected final String m_path;

  protected final PageButtonEventListener m_buttonEventListener;

  protected final TitleFormatter m_titleFormatter = new TitleFormatter();
  protected HorizontalPanel m_titlePanel;

  public ToolBar(PageButtonEventListener pageButtonEventListener, String pagePath) {
    m_buttonEventListener = pageButtonEventListener;
    m_path = pagePath;
    initWidget(createRootWidget());
  }

  private Widget createRootWidget() {
    m_mainPanel.setStyleName(IStyleConstants.MYFIT_VIEW_TOOL_PANEL_STYLE_NAME);
    Widget changeModePanel = createChangeModePanel();

    Widget toolPanel = createToolPanel();
    Widget titlePanel = createTitlePanel();
    VerticalPanel centralPanel = new VerticalPanel();
    centralPanel.setStyleName(IStyleConstants.CENTRAL_TOOL_PANEL_STYLE_NAME);
    centralPanel.add(titlePanel);
    centralPanel.add(toolPanel);

    m_mainPanel.add(changeModePanel);
    m_mainPanel.add(centralPanel);

    m_mainPanel.setCellWidth(changeModePanel, "70px");

    m_mainPanel.setCellHorizontalAlignment(changeModePanel, HasHorizontalAlignment.ALIGN_LOCALE_START);
    m_mainPanel.setCellHorizontalAlignment(centralPanel, HasHorizontalAlignment.ALIGN_LEFT);

    return m_mainPanel;
  }

  private Widget createTitlePanel() {
    m_titlePanel = new HorizontalPanel();
    m_titlePanel.setStyleName(IStyleConstants.TITLE_PANEL_STYLE_NAME);

    return m_titlePanel;
  }

  private Widget createChangeModePanel() {
    m_changeModePanel = new Grid(1, 1);
    m_changeModePanel.setStyleName(IStyleConstants.CHANGE_MODE_PANEL_STYLE_NAME);
    m_editButton = new PageButton("Edit Mode", IStyleConstants.EDIT_BUTTON_STYLE_NAME, PageButtonEventType.TO_EDIT_MODE, m_buttonEventListener);
    m_viewButton = new PageButton("View Mode", IStyleConstants.VIEW_BUTTON_STYLE_NAME, PageButtonEventType.TO_VIEW_MODE, m_buttonEventListener);
    m_changeModePanel.setWidget(0, 0, m_editButton);
    return m_changeModePanel;
  }

  private Widget createToolPanel() {
    m_EditViewPanel = new Grid(1, 1);
    m_EditViewPanel.setStyleName(IStyleConstants.TOOL_PANEL_STYLE_NAME);
    createViewToolPanel();
    createEditToolPanel();
    m_EditViewPanel.setWidget(0, 0, m_viewToolPanel);
    return m_EditViewPanel;
  }

  private void createEditToolPanel() {
    m_editToolPanel = new HorizontalPanel();
    m_editToolPanel.setStyleName(IStyleConstants.EDIT_TOOL_PANEL_STYLE_NAME);
    m_editToolPanel.setSpacing(5);
    m_editToolPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
  }

  private void createViewToolPanel() {
    m_viewToolPanel = new HorizontalPanel();
    m_viewToolPanel.setStyleName(IStyleConstants.VIEW_TOOL_PANEL_STYLE_NAME);
    m_viewToolPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    m_viewToolPanel.setSpacing(5);
  }

  public void setMode(Mode mode) {
    switch (mode) {
      case EDIT:
        setEditMode();
        break;
      case VIEW:
        setViewMode();
        break;
    }
  }

  private void setViewMode() {
    m_changeModePanel.clear();
    m_changeModePanel.setWidget(0, 0, m_editButton);
    m_EditViewPanel.clear();
    m_EditViewPanel.setWidget(0, 0, m_viewToolPanel);
  }

  private void setEditMode() {
    m_changeModePanel.clear();
    m_changeModePanel.setWidget(0, 0, m_viewButton);
    m_EditViewPanel.clear();
    m_EditViewPanel.setWidget(0, 0, m_editToolPanel);
  }
}
